package com.digua.base.socket.response

import java.util.ArrayDeque
import java.util.Queue


class ResponseFactory {
    companion object {
        private const val POOL_SIZE = 7
        private val ERROR_RESPONSE_POOL: Queue<ErrorResponse> = ArrayDeque(POOL_SIZE)
        private val TEXT_RESPONSE_POOL: Queue<TextResponse> = ArrayDeque(POOL_SIZE)
        private val BYTE_BUFFER_RESPONSE_POOL: Queue<ByteBufferResponse> = ArrayDeque(POOL_SIZE)
        private val PING_RESPONSE_POOL: Queue<PingResponse> = ArrayDeque(POOL_SIZE)
        private val PONG_RESPONSE_POOL: Queue<PongResponse> = ArrayDeque(POOL_SIZE)

        fun createErrorResponse() = ERROR_RESPONSE_POOL.poll() ?: ErrorResponse()
        fun createTextResponse() = TEXT_RESPONSE_POOL.poll() ?: TextResponse()
        fun createByteBufferResponse() = BYTE_BUFFER_RESPONSE_POOL.poll() ?: ByteBufferResponse()
        fun createPingResponse() = PING_RESPONSE_POOL.poll() ?: PingResponse()
        fun createPongResponse() = PONG_RESPONSE_POOL.poll() ?: PongResponse()

        fun releaseErrorResponse(response: ErrorResponse) = ERROR_RESPONSE_POOL.offer(response)

        fun releaseTextResponse(response: TextResponse) = TEXT_RESPONSE_POOL.offer(response)

        fun releaseByteBufferResponse(response: ByteBufferResponse) = BYTE_BUFFER_RESPONSE_POOL.offer(response)

        fun releasePingResponse(response: PingResponse) = PING_RESPONSE_POOL.offer(response)

        fun releasePongResponse(response: PongResponse) = PONG_RESPONSE_POOL.offer(response)

    }
}